define(function(require, exports, module) {
	var CONSTANTS=require('resources/js/rescatalogMix/res_constants.js');
	require('resources/js/component/PageGrid.js');
	require('resources/js/jquery.json-2.4.min.js');
	//module.exports={};
	
	var ResPublishMain=function(){};
	
	ResPublishMain.prototype={
		init:function(params){
			$.extend(true,this,params);
			this.render();
			this.bind();
		},	
		bind:function(){
			var that=this;
			$('#btnClear').on('click',function(){
				$('#searchForm input').val('');
				$('#qStatus').val('');
			});
			$('#btnSearch').on('click',function(){
				that.publishList.load(that.getParam());
			});
			$('#btnNewRes').on('click',function(){
				that.jumpView('viewCreate');
			});
			//确定删除按钮
		    $('#btnDeleteSure').click(function(){
		    	$('#deleteDlg').modal('hide');
		    	var name=that.curDeleteName;
		    	that.commandAction('actionDelete',{resPublishId:that.curDeleteId,wfId:that.curWfId},function(){
		    		//删除时，删除我的资源页中对应的我发布的资源下的菜单并关闭相应frame
		    		if(window.parent.Ext)
		    		{
		    			var haha=window.parent.Ext.ComponentQuery.query('panel[title='+myPublishName+']');
		    			if(haha==null)
		    				return;
		    			//删除树节点和关闭iframe
		    			$('tr:has(span:contains('+name+'))',haha[0].body.dom).remove();
		    			var closeA=$('a:has(span:contains('+name+'))',window.parent.document).parent().next('a');
		    			if(closeA&&closeA.length>0) closeA[0].click();
		    			//刷新查询页面
		    			var searchIframe=$('iframe[src*=search]',window.parent.document);
		    			searchIframe.attr('src',searchIframe.attr('src'));
		    			//刷新缓存的权限信息
		    			window.parent.updateAuthority&&window.parent.updateAuthority();
		    		}
		    	});
		   	});
		},
		getParam:function(){
			return {
				userId:this.userId,
				qTitle:$('#qTitle').val(),
				qStatus:$('#qStatus').val()
			};
		},
		getPageParam:function(){
			return $.extend(this.getParam(),{
				pageNo:this.publishList.data.page.pageNo,
				pageSize:this.publishList.data.page.pageSize
			}); 
		},
		curDeleteId:0,
		curDeleteName:'',
		render:function(){
			var that=this;
			//初始化列表
			this.publishList=new PageGrid({
				id:'publishList',
				attachId:'publishList-target',	//创建好的pageGrid要放在那个地方，attachId的标签在创建后会被删除。
				minHeight:'400px',
				checkOpt:false,			//是否有复选框列
				radioOpt:false,
				keyColumn:'resPublishId',		//主键列
				style:{
					pageGrid:'padding:0 5px'
				},
				columns:[{
					title:'资源发布标题',
					code:'title'
				},{
					title:'资源发布人',
					code:'userName'
				},{
					title:'状态',
					code:'status',
					render:function(value,record){
						if(value=='0'&&record.code!='100')
							return '草稿(被驳回)';
						return CONSTANTS.res_publish_status[value];
					}
				}],	//列信息
				buttons:[{
					name:'查看',
					type:'btn-view',
					onclick:function(record){
						$.ajax(that.baseUrl+that.urls['updateCode'],{
							data:{
								resPublishId:record.resPublishId,
								status:record.status
							},
							async:false,
							type:'POST',
							dataType:'json'
						}).done(function(data){
							if(data.ret){
								window.parent&&window.parent.parent&&window.parent.parent.getNum&&window.parent.parent.getNum();
							}
						}).fail(function(){});
						that.jumpView('viewDetail',{
							resPublishId:record.resPublishId,
							status:record.status,
							instanceId:record.instanceId||undefined,
							wfVersionId:record.wfVersionId||undefined,
							taskId:record.taskId||undefined
						});	
					}
				},{
					name:'修改',
					type:'btn-edit',
					onclick:function(record){
						that.jumpView('viewUpdate',{
							resPublishId:record.resPublishId,
							status:record.status,
							instanceId:record.instanceId||0,
							wfVersionId:record.wfVersionId||0,
							taskId:record.taskId||0
						});
					},
					show:function(record){return record.status=='0';}
				},{
					name:'删除',
					type:'btn-delete',
					onclick:function(record){
						that.curDeleteId=record.resPublishId;
						that.curDeleteName=record.bizName;
						that.curWfId=record.instanceId;
						$('#deleteDlg').modal('show');			
					},
					show:function(record){
						return (record.status=='0'&&!record.taskId);//||record.status=='2';
						//return true;
					}
				},{
					name:'发布',
					type:'btn-publish',
					onclick:function(record){
						if(record.code!='100')
						{
							$.ajax(that.baseUrl+that.urls['actionPublish2'],{
								headers: {          
									"Accept" : "text/html; charset=utf-8",         
									"Content-Type": 'application/json;charset=utf-8'  
								},
								data:$.toJSON({
									resPublishId:record.resPublishId,
									status:record.status,
									instanceId:record.instanceId,
									wfVersionId:record.wfVersionId,
									taskId:record.taskId
								}),
								async:false,
								type:'POST',
								dataType:'json'
							}).done(function(data){
								window.parent&&window.parent.parent&&window.parent.parent.getNum&&window.parent.parent.getNum();
								jump(that.baseUrl+that.urls.viewList,that.getPageParam());
							}).fail(function(){});
						}	
						else 
							that.commandAction('actionPublish',{
									resPublishId:record.resPublishId,
									status:record.status,
									instanceId:record.instanceId,
									wfVersionId:record.wfVersionId,
									taskId:record.taskId
								});
					},
					show:function(record){return record.status=='0';}
				},{
					name:'审核',
					type:'btn-audit',
					onclick:function(record){
						that.jumpView('viewVerify',{
							resPublishId:record.resPublishId,
							status:record.status,
							instanceId:record.instanceId,
							wfVersionId:record.wfVersionId,
							taskId:record.taskId
						});
					},
					show:function(record){
						return record.status=='1'&&record.curTransactorId==that.userId;
					}
				}],	//每行的操作按钮
				dataURL:this.baseUrl+this.urls.dataList,	//数据来源的url
				page:{enable:true, pageSize:10,mini:false }, //mini:true pageSize固定
				afterLoadData:function(){},
				onClickRecord:function(){}
			},{param:this.getParam(),page:this.page}); 
		},
		urls:{
			dataList:'/resPublishX/list.jhtml',
			actionDelete:'/resPublish/delete.jhtml',
			actionPublish:'/resPublish/publish.jhtml',
			actionPublish2:'/resPublish/publish2.jhtml',
			viewList:'/resPublishX/main.jhtml',
			viewCreate:'/resPublishX/create.jhtml',
			viewUpdate:'/resPublishX/update.jhtml',
			viewVerify:'/resPublishX/verify.jhtml',
			viewDetail:'/resPublishX/detail.jhtml',
			updateCode:'/resPublish/updateCode.jhtml'
		},
		commandAction:function(action,param,fun){
			var that=this;
			$.ajax(this.baseUrl+this.urls[action],{
				data:param,
				async:false,
				type:'POST',
				dataType:'json'
			}).done(function(data){
				window.parent&&window.parent.parent&&window.parent.parent.getNum&&window.parent.parent.getNum();
				fun&&fun();
				jump(that.baseUrl+that.urls.viewList,that.getPageParam());
			}).fail(function(){});
		},
		jumpView:function(action,param){
			jump(this.baseUrl+this.urls[action],$.extend(this.getPageParam(),param));
		}
	}
	return new ResPublishMain();
});

